﻿using System;
using System.Collections.Generic;
using System.Linq;

using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsTemplateSelectOneDataCommand : CmsTemplateSelectOneCommand
    {
        public override CmsTemplate ExecuteOne()
        {
            CmsTemplate result = null;

            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsTemplate>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsTemplate>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsTemplate, int>>();

                result = SqlUtility<CmsTemplate, int>.ExecuteMultiReader(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                        "cms_sp_TemplateSelectOne",
                        new
                        {
                            TemplateId = CommandParameter.TemplateId,
                            SiteId = CommandParameter.SiteId,
                            SiteGroupId = CommandParameter.SiteGroupId
                        },
                        mapper);
            }

            return result;
        }
    }
}
